/**
 * meta {
 *     title: 导航及nav名称
 *     hidden: 是否显示导航
 *     level: 三级导航的标识
 *     fold: 只有一个导航是否显示折叠效果
 *     lightMenu: 手动跳转的路由 匹配左侧高亮：
 * }
 */

import auth from '@/utils/auth.js';
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router';
import { h, resolveComponent } from 'vue';
import { useMenuList } from '@/pinia/menu.js';
import Layout from '@/components/Layout/index.vue';

const modules = import.meta.glob('../views/**/*.vue');
export const routes = [
  {
    path: '',
    redirect: '/login',
    meta: { title: '登录', hidden: true },
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: '登录', hidden: true },
  },
  {
    path: '/home',
    name: 'home',
    component: Layout,
    meta: { title: '首页', icon: 'icon-home', fold: true },
    redirect: '/home/index',
    children: [
      {
        path: 'index',
        name: 'index',
        component: () => import('@/views/home/index.vue'),
        meta: { title: '首页' },
      },
      {
        path: 'Aiindex',
        name: 'Aiindex',
        component: () => import('@/views/home/Aiindex.vue'),
        meta: { title: 'Ai首页' },
      },
      {
        path: 'callouttasks',
        name: 'callouttasks',
        component: () => import('@/views/callOutTasks/callOutTasks.vue'),
        meta: { title: '外呼任务' },
      },
      {
        path: 'sceneManagement',
        name: 'sceneManagement',
        component: () => import('@/views/sceneManagement/sceneManagement.vue'),
        meta: { title: '场景管理' },
        // children:[

        // ]
      },
      {
        path: 'knowledgeBase',
        name: 'knowledgeBase',
        component: () => import('@/views/sceneManagement/KnowledgeBase.vue'),
        meta: { title: '知识库' },
      },
      {
        path: 'intentionlabel',
        name: 'intentionlabel',
        component: () => import('@/views/intentionlabel/intentionlabel.vue'),
        meta: { title: '意向标签' },
      },
      {
        path: 'customizationlabel',
        name: 'customizationlabel',
        component: () => import('@/views/customizationlabel/customizationlabel.vue'),
        meta: { title: '自定义标签' },
      },
      {
        path: 'dialogVariables',
        name: 'dialogVariables',
        component: () => import('@/views/dialogVariables/dialogVariables.vue'),
        meta: { title: '对话变量' },
      },
      {
        path: 'sceneSettings',
        name: 'sceneSettings',
        component: () => import('@/views/sceneSettings/sceneSettings.vue'),
        meta: { title: '场景设置' },
      },
    ],
  },

  // {
  //     path: '/system',
  //     name: 'system',
  //     component: Layout,
  //     meta: { title: '系统管理', icon: 'icon-systemSet' },
  //     redirect: '/system/menu',
  //     children: [
  //         {
  //             path: 'menu',
  //             name: 'menu',
  //             component: () => import('@/views/system/menu.vue'),
  //             meta: { title: '菜单管理'},
  //         },
  //     ]
  // },
  // {
  //     path: '/qualityTask',
  //     name: 'qualityTask',
  //     component: Layout,
  //     meta: { title: '质检任务', icon: 'icon-task' },
  //     redirect: '/qualityTask/intellectQuality',
  //     children: [
  //         {
  //             path: 'intellectQuality',
  //             name: 'intellectQuality',
  //             component: () => import('@/views/qualityTask/intellectQuality.vue'),
  //             meta: { title: '智能质检任务' },
  //         },
  //         {
  //             path: 'presetManager',
  //             name: 'presetManager',
  //             component: () => import('@/views/qualityTask/presetManager.vue'),
  //             meta: { title: '预设管理' },
  //         }
  //     ]
  // },
  // {
  //     path: '/qualityResult',
  //     name: 'qualityResult',
  //     component: Layout,
  //     meta: {title: '质检结果', icon: 'icon-result', fold: true },
  //     redirect: '/qualityResult/result',
  //     children: [
  //         {
  //             path: 'result',
  //             name: 'result',
  //             component: () => import('@/views/qualityResult/index.vue'),
  //             meta: {title: '质检结果'}
  //         },
  //         {
  //             path: 'qualityDetail',
  //             name: 'qualityDetail',
  //             component: () => import('@/views/qualityResult/qualityDetail.vue'),
  //             meta: { title: '质检详情', hidden: true, lightMenu: '/qualityResult' },
  //         },
  //     ]
  // },
  // {
  //     path: '/qualityForms',
  //     name: 'qualityForms',
  //     component: Layout,
  //     meta: { title: '质检报表', icon: 'icon-forms'},
  //     children: [
  //         {
  //             path: 'wholeForms',
  //             name: 'wholeForms',
  //             component: () => import('@/views/qualityForms/wholeForms.vue'),
  //             meta: { title: '整体报表'}
  //         },
  //         {
  //             path: 'seatsForms',
  //             name: 'seatsForms',
  //             component: () => import('@/views/qualityForms/seatsForms.vue'),
  //             meta: { title: '座席报表'}
  //         },
  //         {
  //             path: 'termForms',
  //             name: 'termForms',
  //             component: () => import('@/views/qualityForms/termForms.vue'),
  //             meta: { title: '质检项报表'}
  //         },
  //         {
  //             path: 'keyWordForms',
  //             name: 'keyWordForms',
  //             component: () => import('@/views/qualityForms/keyWordForms.vue'),
  //             meta: { title: '关键词报表'}
  //         },
  //     ]
  // },
  // {
  //     path: '/dataAnalysis',
  //     name: 'dataAnalysis',
  //     component: Layout,
  //     meta: { title: '数据分析', icon: 'icon-chart' },
  //     children: [
  //         {
  //             path: 'userLabel',
  //             name: 'userLabel',
  //             component: () => import('@/views/dataAnalysis/userLabel.vue'),
  //             meta: { title: '用户标签分析' },
  //         },
  //         {
  //             path: 'seatsLabel',
  //             name: 'seatsLabel',
  //             component: () => import('@/views/dataAnalysis/seatsLabel.vue'),
  //             meta: { title: '座席标签分析' },
  //         }
  //     ]
  // },
  // {
  //     path: '/qualitySet',
  //     name: 'qualitySet',
  //     component: Layout,
  //     meta: { title: '质检设置', icon: 'icon-set' },
  //     redirect: '/qualitySet/qualityTemplate',
  //     children: [
  //         {
  //             path: 'qualityTemplate',
  //             name: 'qualityTemplate',
  //             component: () => import('@/views/qualitySet/qualityTemplate/index.vue'),
  //             meta: { title: '质检模版' },
  //         },
  //         {
  //             path: 'configTemplate',
  //             name: 'configTemplate',
  //             component: () => import('@/views/qualitySet/qualityTemplate/configTemplate.vue'),
  //             meta: { title: '配置质检模版', hidden: true, lightMenu: 'qualityTemplate' },
  //         },
  //         {
  //             path: 'wordsManager',
  //             name: 'wordsManager',
  //             component: {render:() => h(resolveComponent('router-view')) },
  //             meta: { title: '词类管理' },
  //             redirect: '/qualitySet/wordsManager/wordsClass',
  //             children: [
  //                 {
  //                     path: 'wordsClass',
  //                     name: 'wordsClass',
  //                     component: () => import('@/views/qualitySet/wordsManager/wordsClass.vue'),
  //                     meta: { title: '词类', level: true },
  //                 },
  //                 {
  //                     path: 'keyWord',
  //                     name: 'keyWord',
  //                     component: () => import('@/views/qualitySet/wordsManager/keyWord.vue'),
  //                     meta: { title: '关键词', level: true },
  //                 },
  //                 {
  //                     path: 'correct',
  //                     name: 'correct',
  //                     component: () => import('@/views/qualitySet/wordsManager/correct.vue'),
  //                     meta: { title: '转写矫正', level: true },
  //                 }
  //             ]
  //         },
  //         {
  //             path: 'dotManager',
  //             name: 'dotManager',
  //             component: () => import('@/views/qualitySet/dotManager.vue'),
  //             meta: { title: '质检点管理' },
  //         },
  //         {
  //             path: 'labelManager',
  //             name: 'labelManager',
  //             component: () => import('@/views/qualitySet/labelManager.vue'),
  //             meta: { title: '标签管理' },
  //         },
  //     ]
  // },
  // {
  //     path: '/permissionSettings',
  //     name: 'permissionSettings',
  //     component: Layout,
  //     meta: { title: '权限设置', icon: 'icon-lock' },
  //     redirect: '/permissionSettings/userManagement',
  //     children: [
  //         {
  //             path: 'userManagement',
  //             name: 'userManagement',
  //             component: () => import('@/views/permissionSettings/userManagement.vue'),
  //             meta: { title: '用户管理' }
  //         },
  //         {
  //             path: 'roleManagement',
  //             name: 'roleManagement',
  //             component: () => import('@/views/permissionSettings/roleManagement.vue'),
  //             meta: { title: '角色管理' }
  //         },
  //         {
  //             path: 'rolePermission',
  //             name: 'rolePermission',
  //             component: () => import('@/views/permissionSettings/rolePermission.vue'),
  //             meta: { title: '编辑角色权限', hidden: true }
  //         }
  //     ]
  // }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// router.beforeEach(async (to, from,next) => {
//     if(auth.getToken() && useMenuList().asyncRoutes.length > 0) {
//         if(to.path === '/login') {
//             next('/home/index')
//         } else {

//             next()
//         }
//     } else {
//         if(to.path === '/login') {
//             next()
//         } else {
//             next('/login')
//         }
//     }
// })

export default router;

// 处理路由所需格式

// 需要缓存当前页面的路由 目前后端接口不支持
const alivePath = [];

export const generateRouter = (userRouters) => {
  return new Promise((resolve) => {
    userRouters.forEach((i) => {
      i.component = () => import('@/components/Layout/index.vue');
      if (i.meta.hidden === 1) {
        delete i.meta.hidden;
        i.meta.fold = true;
      }
      if (i.children && i.children.length > 0) {
        i.redirect = `${i.path}${i.children[0].path}`;
      }

      i.children &&
        i.children.forEach((item) => {
          item.path = `${i.path}${item.path}`;
          if (item.meta.hidden === 1) {
            item.meta.hidden = true;
          }
          if (alivePath.indexOf(item.path) !== -1) {
            item.meta.keepAlive = true;
          }

          // 判断当前是否是三级菜单
          if (item.children && item.children.length > 0) {
            item.component = { render: () => h(resolveComponent('router-view')) };
            item.redirect = `${item.path}${item.children[0].path}`;
            item.children.forEach((son) => {
              son.meta.level = true;
              son.path = `${item.path}${son.path}`;
              son.component = modules[`../views/${son.component}.vue`];
            });
          } else {
            item.component = modules[`../views/${item.component}.vue`];
          }
        });
    });
    resolve(userRouters);
  });
};

// 添加动态路由
export function addRouteList(data) {
  return new Promise((resolve) => {
    const promises = [];
    data.forEach((e) => promises.push(router.addRoute(e)));
    Promise.all(promises).then(() => resolve(true));
  });
}
